﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Core.BusinessLayer;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web.UserControls
{
    /// <summary>
    /// Employee selection.
    /// </summary>
    /// <remarks></remarks>
    public partial class SelectEmployer : System.Web.UI.UserControl
    {  
        /// <summary>
        /// Gets or sets the selected user ID.
        /// </summary>
        /// <value>The selected user ID.</value>
        /// <remarks></remarks>
        public int? SelectedUserID { get; set; }


        /// <summary>
        /// Occurs when [event employee changed].
        /// </summary>
        /// <remarks></remarks>
        public event EventHandler EventEmployeeChanged;
        //private List<User> allUsers;// = new List<User>();

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                PopulateEmployees(null);

                if (SelectedUserID != null)
                {
                    RadComboBoxEmployee.SelectedValue = SelectedUserID.ToString();
                }
            }
        }

        /// <summary>
        /// Populates the employees2.
        /// </summary>
        /// <param name="users">The users.</param>
        /// <remarks></remarks>
        private void PopulateEmployees2(List<User> users)
        {
            RadComboBoxEmployee.DataSource = users;
            
            RadComboBoxEmployee.DataBind();
        }

        /// <summary>
        /// Populates the employees.
        /// </summary>
        /// <param name="SBU_ID">The SB u_ ID.</param>
        /// <remarks></remarks>
        private void PopulateEmployees(int? SBU_ID)
        {
            UserManager um = new UserManager();

            if (SBU_ID == null)
            {
                RadComboBoxEmployee.DataSource = um.GetAllUsers();
            }
            else
            {
                RadComboBoxEmployee.DataSource = um.GetAllUsersByDepartment((int)SBU_ID);
            }
            RadComboBoxEmployee.DataBind();
        }
        
        /// <summary>
        /// Handles the SelectedIndexChanged event of the RadComboBoxDepartment control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadComboBoxDepartment_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            PopulateEmployees(Int32.Parse(RadComboBoxDepartment.SelectedValue));
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the RadComboBoxEmployee control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        protected void RadComboBoxEmployee_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (EventEmployeeChanged != null)
            {
                EventEmployeeChanged(sender, e);
            }
        }
    }
}